﻿using System;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;

namespace whu2020302051098 {
    public partial class InfrastructureQueryForm : Form
    {
        private AxMapControl mapControl = null;
        private IFeatureLayer buildingFeatureLayer = null;
        private IFeatureClass featureClass = null;

        public InfrastructureQueryForm(AxMapControl aMapControl) {
            InitializeComponent();
            this.mapControl = aMapControl;
            for (var index = 0; index < aMapControl.LayerCount; ++index)
            {
                string name = aMapControl.get_Layer(index).Name;
                if (name == "建筑")
                {
                    this.buildingFeatureLayer = aMapControl.get_Layer(index) as IFeatureLayer;
                    this.featureClass = buildingFeatureLayer.FeatureClass;
                    break;
                }
            }
            type.SelectedIndexChanged += Type_IndexChanged;
            names.SelectedIndexChanged += Names_IndexChanged;
            InitType();
        }

        public void InitType()
        {
            this.type.Items.Add("宿舍");
            this.type.Items.Add("医院");
            this.type.Items.Add("住宅");
            this.type.Items.Add("办事处");
            this.type.Items.Add("体育馆");
            this.type.SelectedIndex = 0;
            
        }


        public void Type_IndexChanged(object o,EventArgs e)
        {
            string whereClause = "";
            switch (type.Text)
            {
                case "宿舍": 
                    whereClause = "type = 'dormitory'";
                    break;
                case "医院":
                    whereClause = "type = 'hospital'";
                    break;
                case "住宅":
                    whereClause = "type = 'residential'";
                    break;
                case "办事处":
                    whereClause = "type = 'office'";
                    break;
                case "体育馆":
                    whereClause = "type = 'stadium'";
                    break;
            }

            this.names.Items.Clear();
            IQueryFilter queryFilter = new QueryFilterClass();
            queryFilter.WhereClause = whereClause; 

            IFeatureCursor pFeatCursor = featureClass.Search(queryFilter, true);
            IFeature pFeat = pFeatCursor.NextFeature(); 

            var idIndex = featureClass.FindField("FID");
            var nameIndex = featureClass.FindField("name");
            while (pFeat != null)
            {
                var name = pFeat.get_Value(nameIndex);
                var id = pFeat.get_Value(idIndex);
                names.Items.Add(id + ": " + name);
                pFeat = pFeatCursor.NextFeature();
            }

            Query(whereClause);
        }

        public void Names_IndexChanged(object o, EventArgs e)
        {
            string FID = names.SelectedItem.ToString().Split(':')[0];
            string whereClause = "FID=" + FID;
            Query(whereClause);
        }


        public void Query(string whereClause)
        {
            try {
                mapControl.Map.ClearSelection();
                IActiveView pActiveView = mapControl.Map as IActiveView;
                //pQueryFilter的实例化 
                IQueryFilter pQueryFilter = new QueryFilterClass();
                //设置查询过滤条件时，大家有时需要手工编辑这个字符串，保证语法关系的正确性，否则就需要在得到字段值时需要判断“数据类型”
                pQueryFilter.WhereClause = whereClause; //需要有规范的格式
                //search的参数第一个为过滤条件，第二个为是否重复执行
                IFeatureCursor pFeatureCursor = featureClass.Search(pQueryFilter, false);
                //获取查询到的要素 
                IFeature pFeature = pFeatureCursor.NextFeature();
                //判断是否获取到要素 
                while (pFeature != null) {
                    mapControl.Map.SelectFeature(buildingFeatureLayer, pFeature); //选择要素 
                    pFeature = pFeatureCursor.NextFeature(); //游标下移
                }

                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                pActiveView.Refresh(); //刷新图层
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }



    }
}
